package page;

import event.CoordinateProcessing;
import event.FileProcessing;
import event.ImageProcessing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class Index extends JFrame implements ActionListener {
    //图片处理类实例
    ImageProcessing imageProcessing = new ImageProcessing();
    CoordinateProcessing coordinateProcessing = new CoordinateProcessing();
    FileProcessing fileProcessing = new FileProcessing();

    //   窗口
    JFrame jf = new JFrame("图片按文件夹归类");

    //    画板
    JPanel jp = new JPanel();


    //    布局选项卡
    JTabbedPane tp = new JTabbedPane();


    //    布局1（容器）
    Container con = new Container();

    //    标签
    JLabel pictureFolder = new JLabel("选择图片文件夹：");
    JLabel outputPath = new JLabel("选择输出路径：");

    //    路径文本
    JTextArea pictureText = new JTextArea();
    JTextArea outputText = new JTextArea();

    //    按钮
    JButton pictureButton = new JButton("...");
    JButton outputButton = new JButton("...");

    /*执行按钮*/
    JButton startButton = new JButton("执行中文名图片处理");


    JButton startTwoButton = new JButton("图片处理-分隔");


    //    ASCII转换
    Container ASCIIConversion = new Container();

    JTextArea asciiText = new JTextArea();
    JTextArea asciiCode = new JTextArea();

    JButton asciiButton = new JButton("转换");

    //    文件夹内图片移出
    Container fileContainer = new Container();

    JLabel fileFolder = new JLabel("选择文件夹：");
    JLabel filePath = new JLabel("选择输出路径：");

    JTextArea inputFileText = new JTextArea();
    JTextArea outFileText = new JTextArea();

    JButton inputFileButton = new JButton("...");
    JButton outFileButton = new JButton("...");

    JButton fileButton = new JButton("执行");


    //    文件选择器
    JFileChooser chooser = new JFileChooser();

    public void Frame() {
        /*设置窗口属性*/
//        窗口可见
        jf.setVisible(true);
//        窗口大小
        jf.setSize(600, 333);
//        关闭窗口时关闭程序
        jf.setDefaultCloseOperation(EXIT_ON_CLOSE);


//        面板加载到窗口
        jf.add(jp);
//        布局卡加载到面板
        jf.setContentPane(tp);

//        设置位置
        pictureFolder.setBounds(20, 20, 120, 20);
        pictureText.setBounds(140, 20, 400, 20);
        pictureButton.setBounds(540, 20, 20, 20);

        outputPath.setBounds(20, 60, 120, 20);
        outputText.setBounds(140, 60, 400, 20);
        outputButton.setBounds(540, 60, 20, 20);

        startButton.setBounds(100, 140, 100, 30);
        startTwoButton.setBounds(300, 140, 150, 30);

//      设置按钮事件处理
        pictureButton.addActionListener(this);
        outputButton.addActionListener(this);
        startButton.addActionListener(this);
        startTwoButton.addActionListener(this);

//        按钮添加进容器
//        图片文件夹
        con.add(pictureFolder);
        con.add(pictureText);
        con.add(pictureButton);
//        输出路径
        con.add(outputPath);
        con.add(outputText);
        con.add(outputButton);
//        添加执行按钮
        con.add(startButton);
        con.add(startTwoButton);

//        将容器放入布局
        tp.add("图片归类", con);


//        ASCII
        asciiText.setBounds(20, 40, 50, 20);
        asciiCode.setBounds(20, 120, 50, 20);

        asciiButton.setBounds(20, 80, 100, 20);
        ASCIIConversion.add(asciiText);
        ASCIIConversion.add(asciiCode);
        ASCIIConversion.add(asciiButton);
        asciiButton.addActionListener(this);

        tp.add("转换为ASCII", ASCIIConversion);

//        文件夹内图片移出

//        设置位置
        fileFolder.setBounds(20, 20, 120, 20);
        inputFileText.setBounds(140, 20, 400, 20);
        inputFileButton.setBounds(540, 20, 20, 20);

        filePath.setBounds(20, 60, 120, 20);
        outFileText.setBounds(140, 60, 400, 20);
        outFileButton.setBounds(540, 60, 20, 20);

        fileButton.setBounds(100, 140, 100, 30);

        fileContainer.add(fileFolder);
        fileContainer.add(inputFileText);
        fileContainer.add(inputFileButton);
        fileContainer.add(filePath);
        fileContainer.add(outFileText);
        fileContainer.add(outFileButton);
        fileContainer.add(fileButton);
        inputFileButton.addActionListener(this);
        outFileButton.addActionListener(this);
        fileButton.addActionListener(this);

        tp.add("文件夹内图片移出", fileContainer);

    }


    @Override
    public void actionPerformed(ActionEvent e) {

//        try {
//            图片整理入文件夹
            if (e.getSource() == pictureButton) {
                pictureText.setText(this.getTextPath());
            } else if (e.getSource() == outputButton) {
                outputText.setText(this.getTextPath());
            } else if (e.getSource() == startButton) {
                System.out.println("执行中文名图片处理");
                imageProcessing.getFiles(pictureText.getText(), outputText.getText());
            } else if (e.getSource() == startTwoButton) {
                System.out.println("图片处理-分隔");
                coordinateProcessing.getFiles(pictureText.getText(), outputText.getText());
            } else if (e.getSource() == asciiButton) {
//                ASCII转换
//                System.out.println("ASCII");
                int ascii = imageProcessing.SumStrAscii(asciiText.getText());
                asciiCode.setText(ascii + "");
                System.out.println(asciiText.getText() + "： " + asciiCode);
            } else if (e.getSource() == inputFileButton) {
//                文件夹内图片导出
                inputFileText.setText(this.getTextPath());
            } else if (e.getSource() == outFileButton) {
                outFileText.setText(this.getTextPath());
            } else if (e.getSource() == fileButton) {
                fileProcessing.removeFiles(inputFileText.getText(), outFileText.getText());

            }

//
//        } catch (Exception ex) {
//            JOptionPane.showConfirmDialog(null, "运行失败，请联系开发者");
//        }

    }

    /**
     * 返回选择的文件夹路径
     *
     * @return string
     */
    public String getTextPath() {
//            设置文件选择器【0：文件 1：文件夹】
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//            打开文件浏览器
        int status = chooser.showOpenDialog(null);
        if (status == JFileChooser.CANCEL_OPTION) {
            System.out.println("选择了取消");
            return null;
        } else {
            File f = chooser.getSelectedFile();
//            text.setText(f.getAbsolutePath());
            System.out.println("主页面获取的文件夹路径" + f.getAbsolutePath());
            return f.getAbsolutePath();
        }
    }
}
